"""
auth:xgt-python
datetimde:2021/11/5
爬取王者荣耀全英雄皮肤图片
"""

import requests
import pprint
import re
import os


url = 'https://pvp.qq.com/web201605/js/herolist.json'
headers ={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40'
}

response = requests.get(url=url,headers=headers)
json_data  = response.json()
for index in json_data:
    hero_id = index['ename']
    hero_name = index['cname']
    path = f'{hero_name}\\'
    if not os.path.exists(path):
        os.mkdir(path)
    index_url = f'https://pvp.qq.com/web201605/herodetail/{hero_id}.shtml'
    response1 = requests.get(url=index_url,headers=headers)
    response1.encoding = response1.apparent_encoding
    naw_list = re.findall('data-imgname="(.*?)"',response1.text)[0]
    # replace()
    name  = re.sub('&\d+','',naw_list).split('|')
    for i in range(1,int(len(name)) +1 ):
        link_url = f'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_id}/{hero_id}-bigskin-{i}.jpg'
        img_name = name[int(i) -1 ]
        img_content = requests.get(url=link_url,headers=headers).content
        with open(path + img_name + '.jpg',mode='wb') as file:
            file.write(img_content)
        print(img_name)